package com.example.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;


/*
 *
 * */
@WebServlet("/ReadCookies")
public class ReadCookies extends HttpServlet {
    private static final long serialVersionUID = 1l;


    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie=null;
        Cookie[] cookies=null;

        cookies=req.getCookies();


        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter out= resp.getWriter();
        if(cookies!=null){
            out.println("Cookie");
            for(int i=0;i< cookies.length;i++){
                cookie=cookies[i];

                if((cookie.getName()).compareTo("name")==0){
                    cookie.setMaxAge(0);
                    resp.addCookie(cookie);
                    out.print("已删除的cookie"+cookie.getName());
                }

                out.print(cookie.getName());
                out.print(URLDecoder.decode(cookie.getValue(),"utf-8"));

            }
        }




    }
}
